package com.wuchao.jdbc.basedao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @Author wuchao66
 * @Date 2023/3/28 13:54
 * @PackageName:com.wuchao.jdbc2.basedao
 * @ClassName: BaseDao
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
public class BaseDaoOracleProperties {
    //javaoop 第一章
    static {//静态块
        init();
    }
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    public static void init(){//初始化赋值，把外面拿进来
        //文件流--本类调用
        InputStream resourceAsStream = BaseDaoOracleProperties.class.getClassLoader().
                getResourceAsStream("oracle.properties");
        //得到属性操作对象
        Properties properties = new Properties();
        //文件流加载进来
        try {
            properties.load(resourceAsStream);
            driver=properties.getProperty("driver");
            url=properties.getProperty("url");
            username=properties.getProperty("username");
            password=properties.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public Connection getConnection(){
        Connection connection=null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
            //connection.setAutoCommit(false);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
           // connection.rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    public  void closeAll(Connection connection, PreparedStatement preparedStatement,
                          ResultSet resultSet, CallableStatement callableStatement){
        //存储过程 调用的 预处理
        try {
            if(connection!=null&&!connection.isClosed()){
                connection.close();
            }if(preparedStatement!=null&&!preparedStatement.isClosed()){
                preparedStatement.close();
            }if(resultSet!=null&&!resultSet.isClosed()){
                resultSet.close();
            }if(callableStatement!=null&&!callableStatement.isClosed()){
                callableStatement.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
